﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.ComponentModel;
using Leiktuther.ViewModels;

namespace Leiktuther.Models
{
    public class PlayerRepository 
    {
        private IAppRepository _repository = null;
        private AppDataContext _db = new AppDataContext();

        public PlayerRepository()
        {
            _repository = new AppRepository();
        }

        public PlayerRepository(IAppRepository rep)
        {
            _repository = rep;
        }

        public IEnumerable<Player> GetAllPlayers()
        {
            return _db.Players.ToList();
            /* List<MembershipUser> allOfThem = new List<MembershipUser>();
            return null; */
        }

        public Player GetPlayerById(int? id)
        {
            var player = (from pl in _repository.Players
                         where pl.ID == id
                         select pl).SingleOrDefault();
            return player;
        }

        public void RemovePlayerByString(string userinn)
        {
            using (var db = new AppDataContext())
            {
                Player pl = db.Players.First(i => i.UserName == userinn);
                db.Players.Remove(pl);
                db.SaveChanges();
                Membership.DeleteUser(userinn);
            }
        }

        public void AddAdmin(string userinn)
        {
            AppDataContext db = new AppDataContext();

            Player player = (from p in db.Players
                          where p.UserName == userinn
                          select p).SingleOrDefault();
            player.Category = "Admin";
            db.SaveChanges();
            
            //IEnumerable<Player> all = GetAllPlayers();
            //Player user = all.Where( p => p.UserName == userinn );
        }
        /*
        public void SetCategory(string userinn)
        {
            AppDataContext db = new AppDataContext();

            var player = (from p in _db.Players
                          where p.UserName == userinn
                          select p).SingleOrDefault();
            player.Category = "Playa";
            db.SaveChanges();
        }*/
        
        public void AddPlayer(MembershipUser member, string password)
        {
            string userId = member.ProviderUserKey.ToString();
            Guid guidId = new Guid(userId);
            Player playa = new Player
            {
                UserName = member.UserName,
                Email = member.Email,
                UserID = guidId,
                Category = "Playa",
                Password = password
            };
            using (var db = new AppDataContext())
            {
                    db.Players.Add(playa);
                    db.SaveChanges();
            }
        }

        /*
        public void AddPlayer(Player player)
        {
            _db.Players.Add(player);
            _db.SaveChanges();
        }*/

    }
}